home *** CD-ROM | disk | FTP | other *** search
- Path: maverick.tad.eds.com!news-admin@tad.eds.com
- From: gulleha@vaxixhp3.vaxix.slg.eds.com (Harold Guller)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: 15 Feb 1996 20:11:11 GMT
- Organization: EDS
- Message-ID: <4g040v$7jj@maverick.tad.eds.com>
- References: <11f7cc$17261a.3b3@daprez> <9602011151.AA04526@dxmint.cern.ch>
- Reply-To: gulleha@vaxixhp3.vaxix.sig.eds.com
- NNTP-Posting-Host: va057.vaxix.slg.eds.com
- X-Newsreader: WinVN 0.91.3
-
- In article <9602011151.AA04526@dxmint.cern.ch>, danpop@mail.cern.ch (Dan Pop) says:
- >>int main (int argc, char **argv) {
- >>
- >> if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
- >> Usage();
- >> }
- >...
- >>
- >>Can someone see what's wrong with this ?
- >>am I not using strcmp() right ?
- >
- >Right :-)
- >
- >Your code is equivalent to:
- >
- > if (strcmp(argv[1],"-d") == 0 || strcmp(argv[1],"-e") == 0) Usage();
- >
- >Which means that any time the program is correctly invoked, it will call
- >Usage!
- >
- >So:
- >
- >1. Never use ! with strcmp. It's a good way to shoot yourself in the foot.
-
- You can say that about anything that someone does not understad.
- A better solution is to learn the concept. The "!" operator is widely
- used and should be understood.
-
-
- >2. Always check what a function is supposed to return before using it.
- > strcmp returns 0 when the strings are equal.
-
- >3. Are you sure you didn't mean && when you wrote ||? Even if !strcmp()
- > would have worked as you thought it did, the || operator would have
- > caused the expression inside the if statement to _always_ evaluate to 1.
-
- Why do you advise learning the "&&" and "||" conditions? These are more
- complex than the "!" operator, and he did "shoot himself in the foot".
- Shoudn't he use nested if else contrcuts to achieve his purpose?
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-